import tools from '../../../utils/tools';
import Handle from '../Cart/Handle';

//专门管理绑定事件处理函数
export default class Event {
  constructor(oCart, courseData, cartData, observer) {
    this.oCart = oCart;
    this.courseData = courseData;
    this.cartData = cartData;
    this.observer = observer;

    this.init();
  }

  init() {
    this.bindEvent();
    this.handle = new Handle(this.courseData, this.cartData, this.oCart, this.observer);
  }

  bindEvent() {
    this.oCart.addEventListener('click', this.onRemoveBtnClick.bind(this), false);
  }

  onRemoveBtnClick(ev) {
    const tar = tools().getTarget(ev),
      className = tar.className;

    if (className === 'remove-btn') {
      const id = tar.getAttribute('data-id');
      // console.log(id); 1

      //单独执行观察者函数
      //handleCartItem() 增加或删除某一项
      //参数1：当前环境this 
      //参数2：handle底下handleCartItem所需要的参数
      this.observer.notify(this, id, tar, 0);

    }
  }
}